package com.idanchuang.component.redis;

import com.idanchuang.component.core.helper.EnvContext;
import com.idanchuang.component.redis.util.RedisUtil;
import com.idanchuang.component.shadow.core.ShadowContextMonitor;
import com.idanchuang.support.etcache.EtCacheCoreAutoConfiguration;
import com.idanchuang.support.etcache.pressure.PressureChecker;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @author yjy
 * @date 2019/11/27 11:09
 **/
@Configuration
@AutoConfigureBefore(EtCacheCoreAutoConfiguration.class)
public class ComponentRedisAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(RedisUtil.class)
    public RedisUtil redisUtil(StringRedisTemplate strTemplate, RedisTemplate<Object, Object> objTemplate) {
        return new RedisUtil(strTemplate, objTemplate);
    }

    @Bean
    @ConditionalOnMissingBean(PressureChecker.class)
    public PressureChecker pressureChecker() {
        return () -> EnvContext.isPressure() || ShadowContextMonitor.isShadowEnvironment();
    }

}
